var e = require;
var t = module;
var o = exports;
var ii, nn = this && this.__extends || (ii = function (t, e) {
    return (ii = Object.setPrototypeOf || {
        __proto__: []
    }
        instanceof Array &&
        function (t, e) {
            t.__proto__ = e
        } ||
        function (t, e) {
            for (var o in e) Object.prototype.hasOwnProperty.call(e, o) && (t[o] = e[o])
        })(t, e)
},
    function (t, e) {
        function o() {
            this.constructor = t
        }
        ii(t, e),
            t.prototype = null === e ? Object.create(e) : (o.prototype = e.prototype, new o)
    }
),
    rr = this && this.__awaiter ||
        function (t, e, o, i) {
            return new (o || (o = Promise))(function (n, a) {
                function r(t) {
                    try {
                        l(i.next(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function s(t) {
                    try {
                        l(i.
                            throw(t))
                    } catch (e) {
                        a(e)
                    }
                }
                function l(t) {
                    var e;
                    t.done ? n(t.value) : (e = t.value, e instanceof o ? e : new o(function (t) {
                        t(e)
                    })).then(r, s)
                }
                l((i = i.apply(t, e || [])).next())
            })
        };
var oo = (this && this.__decorate) ||
    function (e, t, a, n) {
        var i,
            o = arguments.length,
            s = o < 3 ? t : null === n ? (n = Object.getOwnPropertyDescriptor(t, a)) : n;
        if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) s = Reflect.decorate(e, t, a, n);
        else
            for (var r = e.length - 1; r >= 0; r--)
                (i = e[r]) && (s = (o < 3 ? i(s) : o > 3 ? i(t, a, s) : i(t, a)) || s);
        return o > 3 && s && Object.defineProperty(t, a, s), s;
    };
Object.defineProperty(o, "__esModule", { value: !0 });
var a = e("BaseModuleUI"),
    n = e("NotifyEventType"),
    i = e("GlobalParams"),
    r = e("AssetsManager"),
    s = e("AudioManager"),
    l = e("CommonHelper"),
    _ = e("EquipInfo"),
    c = e("Game"),
    d = e("RoleSoldierInfo"),
    h = e("sdkConfig"),
    u = e("AD"),
    p = e("BuyEquipInfo"),
    m = e("node_team"),
    f = e("ReviveViewUI"),
    v = e("role_node"),
    g = e("small_map_node"),
    y = e("top_game_player"),
    I = cc._decorator,
    w = I.ccclass,
    b = I.property,
    k = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t.left_base_node = null),
                (t.right_base_node = null),
                (t.role_soldier_view = null),
                (t.player_info_tip_view = null),
                (t.game_player_node = null),
                (t.game_over_anime = null),
                (t.small_map_node = null),
                (t.fight_top_node = null),
                (t.roleStartPos = [
                    cc.v2(-4270, -20),
                    cc.v2(4270, -20),
                    cc.v2(-4560, -40),
                    cc.v2(4560, -40),
                    cc.v2(-4400, -90),
                    cc.v2(4400, -90),
                    cc.v2(-4270, -220),
                    cc.v2(4270, -220),
                    cc.v2(-4560, -200),
                    cc.v2(4560, -200)
                ]),
                (t.revive_view_node = null),
                (t.ai_num_equip_info = []),
                t
            );
        }
        return (
            nn(t, e),
            (t.prototype.initView = function () {
                var t = this;
                e.prototype.initView.call(this),
                    u.default.getInstance().hideBanner(),
                    h.default.platform != h.EPlatform.TikTok && u.default.getInstance().showBanner(),
                    (this.fight_top_node.active = !1),
                    s.default.getInstance().setMusicVolume(1),
                    (i.GlobalParams.gameMusicName = "fight_bgm"),
                    s.default.playGameBGM(),
                    (i.GlobalParams.PlayingRoleDie = []),
                    (i.GlobalParams.GameDestroyTower = [
                        [0, 0, 0],
                        [0, 0, 0]
                    ]),
                    (i.GlobalParams.MonsterDisNum = [
                        { num: 0, time: 0, exist: !1, type: c.MonsterType.red },
                        { num: 0, time: 0, exist: !1, type: c.MonsterType.bule },
                        { num: 0, time: 0, exist: !1, type: c.MonsterType.bule },
                        { num: 0, time: 0, exist: !1, type: c.MonsterType.red },
                        { num: 0, time: 0, exist: !1, type: c.MonsterType.dragon },
                        { num: 0, time: 0, exist: !1, type: c.MonsterType.spider },
                        { num: 0, time: 1800, exist: !1, type: c.MonsterType.big_dragon }
                    ]),
                    (i.GlobalParams.BuyEquipInfo = []),
                    (i.GlobalParams.GamePlayerInfo = []),
                    (i.GlobalParams.PlayerJoinFight = !0),
                    this.setGamePlayerInfo(),
                    this.createRole(),
                    this.getAiMonEquipInfo(),
                    (this.game_over_anime.node.parent.active = !1);
                for (var o = 0; o < 5; o++)
                    this.game_player_node.children[o]
                        .getComponent(y.default)
                        .showPlayerInfo(i.GlobalParams.MatchMyIndex % 2 == 0 ? o + o : o + o + 1),
                        this.game_player_node.children[o + 5]
                            .getComponent(y.default)
                            .showPlayerInfo(i.GlobalParams.MatchMyIndex % 2 != 0 ? o + o : o + o + 1);
                this.scheduleOnce(function () {
                    t.emitEvent(
                        n.default.SEND_SHOW_GAME_MAP_INFO,
                        i.GlobalParams.MatchMyIndex % 2 == 0 ? t.right_base_node : t.left_base_node
                    ),
                        t.scheduleOnce(function () {
                            t.gameStart();
                        }, 5);
                }, 1);
            }),
            (t.prototype.gameStart = function () {
                var e = this;
                (i.GlobalParams.GameGiveUpTime = new Date().getTime() + 18e4),
                    s.default.getInstance().playFx("three_time"),
                    e.showTopTip(0),
                    e.scheduleOnce(function () {
                        (i.GlobalParams.GameStart = !0),
                            (i.GlobalParams.GamePlayTime = new Date().getTime()),
                            h.default.platform == h.EPlatform.TikTok &&
                            (u.default.getInstance().StopRecorder(), u.default.getInstance().StartRecorder()),
                            e.createSoldier(),
                            e.FightCountDown(),
                            s.default.getInstance().playFx("army_attack"),
                            e.showTopTip(1);
                    }, 4);
            }),
            (t.prototype.showTopTip = function (e) {
                var t = this;
                (this.fight_top_node.active = !0),
                    this.fight_top_node.setPosition(cc.v2(0, 250)),
                    (this.fight_top_node.children[0].active = 0 == e),
                    (this.fight_top_node.children[1].active = 1 == e),
                    cc
                        .tween(this.fight_top_node)
                        .to(2, { position: cc.v2(0, 300) })
                        .call(function () {
                            t.fight_top_node.setPosition(cc.v2(0, 250)), (t.fight_top_node.active = !1);
                        })
                        .start();
            }),
            (t.prototype.addEvent = function () {
                this.attachEvent(n.default.SEND_ROLE_BE_KILL_INFO, this.updatePlayerKillInfo, this),
                    this.attachEvent(n.default.SEND_GAME_OVER_INFO, this.showGameOver, this),
                    this.attachEvent(n.default.SEND_PLAYER_SHOOT_WOINDE_ANIME, this.showWoindeAnime, this);
            }),
            (t.prototype.showWoindeAnime = function () {
                this.node.stopAllActions(),
                    (this.node.angle = 0),
                    cc.tween(this.node).by(0.05, { angle: 0.5 }).by(0.1, { angle: -1 }).by(0.05, { angle: 0.5 }).start();
            }),
            (t.prototype.FightCountDown = function () {
                var e = this;
                if (i.GlobalParams.GameStart) {
                    i.GlobalParams.PlayingRoleDie.length > 0 && this.updatePlayingDie();
                    for (var t = 0; t < i.GlobalParams.MonsterDisNum.length; t++) {
                        var o = i.GlobalParams.MonsterDisNum[t];
                        if (0 != o.time || o.exist) {
                            if (o.time > 0 && !o.exist) o.time--, this.small_map_node.showMonsterTime(t, o.time);
                            else if (-1 == o.time) continue;
                        } else (o.exist = !0), this.emitEvent(n.default.SEND_CREATE_MONSTER_INDEX, t);
                    }
                    this.scheduleOnce(function () {
                        e.FightCountDown();
                    }, i.GlobalParams.GameUpdateTime);
                }
            }),
            (t.prototype.createSoldier = function () {
                var e = this;
                this.emitEvent(n.default.SEND_CREATE_SOLDIER_INFO),
                    this.scheduleOnce(function () {
                        e.createSoldier();
                    }, 30);
            }),
            (t.prototype.createRole = function () {
                for (var e = 0; e < i.GlobalParams.MatchGamePlayer.length; e++) this.creationRole(e);
            }),
            (t.prototype.updatePlayingDie = function () {
                for (var e = i.GlobalParams.PlayingRoleDie.length - 1; e > -1; e--) {
                    var t = i.GlobalParams.PlayingRoleDie[e],
                        o = t.role_index;
                    t.cound_down--;
                    var a = this.game_player_node.getChildByName("game_player_" + o);
                    (a.active = !0),
                        a.getComponent(y.default).updatePlayerInfo(t.cound_down),
                        this.revive_view_node &&
                        o == i.GlobalParams.MatchMyIndex &&
                        (this.revive_view_node.getComponent(f.default).showReviveTime(t.cound_down),
                            t.cound_down <= 0 &&
                            (h.default.platform == h.EPlatform.TikTok &&
                                (u.default.getInstance().hideBanner(), u.default.getInstance().ResumeRecorder()),
                                this.revive_view_node.getComponent(f.default).closeThisPopup())),
                        t.cound_down <= 0 &&
                        (o % 2 != i.GlobalParams.MatchMyIndex % 2 && (a.active = !1),
                            this.creationRole(o),
                            i.GlobalParams.PlayingRoleDie.splice(e, 1));
                }
            }),
            (t.prototype.creationRole = function (e) {
                var t = r.default.getInstance().getFightPrefab("role_" + i.GlobalParams.MatchGamePlayer[e].role_id),
                    o = cc.instantiate(t);
                o.name = "role_team_" + e;
                var a = this.roleStartPos[e];
                (o.scaleX = e % 2 == 0 ? -1 : 1),
                    o.setPosition(a),
                    o
                        .getComponent(m.default)
                        .setTeamInfo(-1 == o.scaleX ? c.TeamInfo.left_team : c.TeamInfo.right_team),
                    o.getComponent(v.default).showRoleInfo(e != i.GlobalParams.MatchMyIndex, e);
                var n = cc.instantiate(r.default.getInstance().getFightPrefab("player_info_node"));
                (n.zIndex = e == i.GlobalParams.MatchMyIndex ? 11 : e),
                    o.getComponent(v.default).setPlayerInfoNode(n),
                    n.setParent(this.player_info_tip_view),
                    o.setParent(this.role_soldier_view);
            }),
            (t.prototype.updatePlayerKillInfo = function (e, t) {
                var o = t;
                o.kill < 11 &&
                    o.be_kill < 11 &&
                    o.kill > -1 &&
                    o.be_kill > -1 &&
                    (i.GlobalParams.MatchGamePlayer[o.kill].kill_info[0]++,
                        h.default.platform == h.EPlatform.TikTok &&
                        o.kill == i.GlobalParams.MatchMyIndex &&
                        u.default.getInstance().RecordClip(5, 5)),
                    o.be_kill < 11 && i.GlobalParams.MatchGamePlayer[o.be_kill].kill_info[1]++,
                    o.help_kill &&
                    -1 != o.help_kill &&
                    o.be_kill < 11 &&
                    o.help_kill < 11 &&
                    (i.GlobalParams.MatchGamePlayer[o.help_kill].kill_info[2]++,
                        (i.GlobalParams.MatchGamePlayer[o.help_kill].exp_num += 117),
                        (i.GlobalParams.MatchGamePlayer[o.help_kill].coin += 100));
                for (var a = 0; a < i.GlobalParams.MatchGamePlayer.length; a++) {
                    var s = i.GlobalParams.MatchGamePlayer[a];
                    if (o.team != c.TeamInfo.rest_team) {
                        if ((o.team - 1) % 2 != a % 2)
                            if (o.be_kill < 11)
                                (s.exp_num += 300 * (o.kill == a ? 1 : 0.5)), (s.coin += 300 * (o.kill == a ? 1 : 0.5));
                            else
                                switch (o.be_kill) {
                                    case c.KillType.near_soldier:
                                        (s.exp_num += 20), (s.coin += 40);
                                        break;
                                    case c.KillType.far_soldier:
                                        (s.exp_num += 30), (s.coin += 65);
                                        break;
                                    case c.KillType.super_soldier:
                                        (s.exp_num += 100 * (o.kill == a ? 1 : 0.5)),
                                            (s.coin += 150 * (o.kill == a ? 1 : 0.5));
                                        break;
                                    case c.KillType.tower:
                                        (s.exp_num += 150), (s.coin += 200);
                                }
                    } else
                        switch (o.be_kill) {
                            case c.KillType.near_soldier:
                                (s.exp_num += 10), (s.coin += 20);
                                break;
                            case c.KillType.far_soldier:
                                (s.exp_num += 15), (s.coin += 30);
                                break;
                            case c.KillType.super_soldier:
                                (s.exp_num += 50), (s.coin += 50);
                                break;
                            case c.KillType.tower:
                                (s.exp_num += 50), (s.coin += 80);
                                break;
                            case c.KillType.red_buff:
                            case c.KillType.blue_buff:
                                (s.exp_num += 100 * (o.kill == a ? 1 : 0.1)), (s.coin += 120 * (o.kill == a ? 1 : 0.1));
                                break;
                            case c.KillType.spider:
                                (s.exp_num += 150), (s.coin += 150);
                                break;
                            case c.KillType.dragon:
                                (s.exp_num += 200), (s.coin += 250);
                                break;
                            case c.KillType.big_dragon:
                                (s.exp_num += 300), (s.coin += 400);
                        }
                    if (s.exp_num >= c.default.level_exp[s.level - 1]) {
                        if (
                            ((s.exp_num -= c.default.level_exp[s.level - 1]),
                                s.level++,
                                a != i.GlobalParams.MatchMyIndex)
                        ) {
                            var _ = l.default.getRandomInt(0, 2);
                            (_ = s.skill_info[_] < 5 ? _ : 0 == _ ? 1 : 0), s.skill_info[_]++;
                            var p = i.GlobalParams.MatchGamePlayer[a];
                            0 == _
                                ? (i.GlobalParams.GamePlayerInfo[a].skill_1 +=
                                    d.default.Role_info[p.role_id].S1_attack_up)
                                : 1 == _
                                    ? (i.GlobalParams.GamePlayerInfo[a].skill_2 +=
                                        d.default.Role_info[p.role_id].S2_attack_up)
                                    : 2 == _ &&
                                    (i.GlobalParams.GamePlayerInfo[a].skill_3 +=
                                        d.default.Role_info[p.role_id].S3_attack_up);
                        }
                        this.playerLevelUp(a), this.emitEvent(n.default.SEND_PLAYER_LEVEL_UP_INFO, a);
                    }
                }
                this.aiBuyEquipInfo(),
                    this.emitEvent(n.default.SEND_UPDATE_MATCH_PLAYER_INFO),
                    o.be_kill == i.GlobalParams.MatchMyIndex &&
                    ((this.revive_view_node = cc.instantiate(
                        r.default.getInstance().getFightPrefab("ReviveViewUI")
                    )),
                        this.node.parent.addChild(this.revive_view_node));
            }),
            (t.prototype.showGameOver = function () {
                s.default.getInstance().playFx(i.GlobalParams.GamePlayerWin ? "game_win" : "game_loss"),
                    (this.game_over_anime.node.parent.active = !0),
                    this.game_over_anime.setAnimation(0, i.GlobalParams.GamePlayerWin ? "sl" : "sb", !1),
                    i.GlobalParams.gameShock.value && u.default.getInstance().vibrateShort(),
                    this.scheduleOnce(function () {
                        h.default.platform == h.EPlatform.TikTok && u.default.getInstance().StopRecorder(),
                            cc
                                .find("Canvas")
                                .addChild(cc.instantiate(r.default.getInstance().getFightPrefab("GameOverViewUI")));
                    }, 3);
            }),
            (t.prototype.getAiMonEquipInfo = function () {
                for (var e = 0; e < i.GlobalParams.MatchGamePlayer.length; e++) {
                    for (
                        var t = _.default.equip_nominate_info[i.GlobalParams.MatchGamePlayer[e].role_id], o = [], a = 0;
                        a < t.nom_equip.length;
                        a++
                    ) {
                        for (var n = _.default.eqquip_info[t.nom_equip[a]], r = 0; r < n.eq_need.length; r++)
                            o.push(n.eq_need[r]);
                        o.push(t.nom_equip[a]);
                    }
                    e != i.GlobalParams.MatchMyIndex
                        ? this.ai_num_equip_info.push(o)
                        : (this.ai_num_equip_info.push([]), (i.GlobalParams.MyNomEquipInfo = o));
                }
            }),
            (t.prototype.aiBuyEquipInfo = function () {
                for (
                    var e = this,
                    t = function (t) {
                        if (o.ai_num_equip_info[t].length > 0) {
                            for (
                                var a = _.default.eqquip_info[o.ai_num_equip_info[t][0]].eq_sale,
                                r = i.GlobalParams.MatchGamePlayer[t].equip_info,
                                s = [],
                                l = _.default.eqquip_info[o.ai_num_equip_info[t][0]],
                                c = 0;
                                c < l.eq_need.length;
                                c++
                            )
                                -1 != r.indexOf(l.eq_need[c]) &&
                                    ((a -= _.default.eqquip_info[l.eq_need[c]].eq_sale),
                                        s.push(r.indexOf(l.eq_need[c])));
                            if (i.GlobalParams.MatchGamePlayer[t].coin >= a) {
                                for (c = 0; c < s.length; c++) r[s[c]] = 0;
                                (r[r.indexOf(0)] = o.ai_num_equip_info[t][0]),
                                    (i.GlobalParams.MatchGamePlayer[t].coin -= a),
                                    (i.GlobalParams.MatchGamePlayer[t].equip_info = r),
                                    o.ai_num_equip_info[t].splice(0, 1),
                                    p.default.playerBuyEquipInfo(t, function () {
                                        e.emitEvent(n.default.SEND_UPDATE_BUY_EQUIP_NEW_ADD_INFO, t);
                                    });
                            }
                        }
                    },
                    o = this,
                    a = 0;
                    a < this.ai_num_equip_info.length;
                    a++
                )
                    t(a);
            }),
            (t.prototype.setGamePlayerInfo = function () {
                p.default.buyEquipInit();
                for (var e = 0; e < i.GlobalParams.MatchGamePlayer.length; e++) {
                    var t = i.GlobalParams.MatchGamePlayer[e],
                        o = d.default.Role_info[t.role_id],
                        a = p.default.getPlayerInit(),
                        n = i.GlobalParams.PlayGuideMode && e % 2 != i.GlobalParams.MatchMyIndex % 2;
                    (a.attack = o.C_attack * (n ? 0.8 : 1)),
                        (a.skill_1 = o.S1_attack * (n ? 0.8 : 1)),
                        (a.skill_2 = o.S2_attack * (n ? 0.8 : 1)),
                        (a.skill_3 = o.S3_attack * (n ? 0.8 : 1)),
                        (a.hp = o.C_hp * (n ? 0.8 : 1)),
                        (a.move_speed = 40),
                        (a.attack_speed = 1),
                        (a.cool_1 = d.default.Hore_Skill_Info[t.role_id].one_time),
                        (a.cool_2 = d.default.Hore_Skill_Info[t.role_id].two_time),
                        (a.cool_3 = d.default.Hore_Skill_Info[t.role_id].three_time),
                        0 == t.dower_info[0]
                            ? (a.attack += t.dower_info[1])
                            : 1 == t.dower_info[0]
                                ? (a.move_speed += t.dower_info[1])
                                : 2 == t.dower_info[0]
                                    ? (a.hp += t.dower_info[1])
                                    : 3 == t.dower_info[0]
                                        ? (a.attack_speed -= t.dower_info[1])
                                        : 4 == t.dower_info[0] && ((a.cool_1 -= t.dower_info[1]), (a.cool_2 -= t.dower_info[1])),
                        i.GlobalParams.GamePlayerInfo.push(a);
                }
            }),
            (t.prototype.playerLevelUp = function (e) {
                var t = i.GlobalParams.MatchGamePlayer[e],
                    o = d.default.Role_info[t.role_id];
                (i.GlobalParams.GamePlayerInfo[e].attack += o.C_attack_up),
                    (i.GlobalParams.GamePlayerInfo[e].hp += o.C_hp_up);
            }),
            oo([b(cc.Node)], t.prototype, "left_base_node", void 0),
            oo([b(cc.Node)], t.prototype, "right_base_node", void 0),
            oo([b(cc.Node)], t.prototype, "role_soldier_view", void 0),
            oo([b(cc.Node)], t.prototype, "player_info_tip_view", void 0),
            oo([b(cc.Node)], t.prototype, "game_player_node", void 0),
            oo([b(sp.Skeleton)], t.prototype, "game_over_anime", void 0),
            oo([b(g.default)], t.prototype, "small_map_node", void 0),
            oo([b(cc.Node)], t.prototype, "fight_top_node", void 0),
            oo([w], t)
        );
    })(a.default);
o.default = k;
